package com.ytzq.itom.function;

import java.sql.SQLException;

import com.ytzq.base.config.Config;
import com.ytzq.base.constants.CONFIG;
import com.ytzq.base.constants.ERROR_CODE;
import com.ytzq.base.constants.LOG_TYPE;
import com.ytzq.base.function.Function;
import com.ytzq.base.function.RequestParameter;
import com.ytzq.base.function.ResponseParameter;
import com.ytzq.base.helper.StringHelper;
import com.ytzq.base.log.Logger;

/**
 * 功能号转发服务入口
 * @author 门士松  20121031
 * @version 1.0
 * @since
 */
public class ForwardFunction{
	private Logger logger = Logger.getLogger(this.getClass());
	 /**
     * 主方法
     */
    public ResponseParameter execute(RequestParameter requestParameter){
    	ResponseParameter responseParameter = new ResponseParameter();
        try{	
            //判断是否已经登录或不需要登陆
            if(isLogin(requestParameter)){
    			logger.debug(LOG_TYPE.BUSINESS,"进入具体功能号："+requestParameter.getFuncNo());			
	            String className = Config.getString(CONFIG.FUNCTION_PACKAGE_NAME)+".F" + requestParameter.getFuncNo();
	            Function functionObj = (Function) (Class.forName(className).newInstance());
	            responseParameter = functionObj.service(requestParameter);
            }else {
    			responseParameter.setErrorCodeAndInfo(ERROR_CODE._2);         	
            }
        }
        catch (Exception e){
			logger.error(LOG_TYPE.BUSINESS, e);
			responseParameter.setErrorCodeAndInfo(ERROR_CODE._1);
        }
        return responseParameter;
    }
    /**
     * 判断是否已经登录或不需要登陆
     * @throws SQLException 
     */
    public boolean isLogin(RequestParameter requestParameter) throws SQLException  {	
    	String functions =  Config.getString(CONFIG.NO_LOGIN_FUNCTION);
    	//不需要登录判断
    	if(!StringHelper.isEmpty(functions) && functions.indexOf(requestParameter.getFuncNo())>=0 ){
			return true;
	    }else{
//	    	(User)requestParameter.getRequest().getSession().getAttribute("user")
//    		return userService.isLogin(requestParameter.getUserID(), requestParameter.getSessionID());
	    	return false;
    	}
    }

}
